//
//  HotelDetailVC.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/11/27.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class HotelDetailVC: BaseVC {

    @IBOutlet weak var tableView: BaseTableView!
    
    private let headHeight = kScreenWidth * 9 / 16
    private var headView: PagerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    private func setupUI() {
        
        self.title = "酒店详情"
        
        setupHeadView()
        self.closeAutomaticallyInsets(scrollview: tableView)
        self.setNavBarStyle(.transparency)
        
        tableView.register(UINib.init(nibName: "HotelDetailTitleCell", bundle: nil), forCellReuseIdentifier: "HotelDetailTitleCell")
        tableView.register(UINib.init(nibName: "HotelContactCell", bundle: nil), forCellReuseIdentifier: "HotelContactCell")
        tableView.register(UINib.init(nibName: "HotelFacilitiesCell", bundle: nil), forCellReuseIdentifier: "HotelFacilitiesCell")
        
        
        
    }
    
    private func setupHeadView() {
        
        var picArray:[Any] = []
        
        for i in 6..<10 {
            
            if let image = UIImage(named: "首页\(i).jpg") {
                
                picArray.append(image)
            }
        }
        
        let frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: headHeight)
        headView = PagerView(frame: frame)
        
        headView.pics = picArray
        
        tableView.tableHeaderView = headView
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        
        headView.selectIndex = {
            
            [weak self] index in
            guard let `self` = self else { return }
            
            self.setupBrowser(images: picArray, index: index, browserView: nil)
        }
    }
    
    var contact:NSAttributedString? {
        
        let att = NSMutableAttributedString()
        
        let tel = "+06 01 97634179"
        if tel.count > 0 {
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "电话")!, fontSize: 10)
            
            att.append(imageAtt!)
            
            let subAtt = NSMutableAttributedString(string: "  \(tel)")
            
            subAtt.yy_setTextHighlight(subAtt.yy_rangeOfAll(), color: nil, backgroundColor: nil) { _,_,_,_ in
                
                Util.phoneAction(tel: tel)
            }
            
            att.append(subAtt)
        }
        
        let address = "劳卡拉岛—斐济Laucala Island Resort"
        if address.count > 0 {
            
            if att.length > 0 {
                
                att.yy_appendString("\n")
            }
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "地址")!, fontSize: 10)
            att.append(imageAtt!)
            
            att.yy_appendString("  \(address)")
        }
        
        if att.length == 0 {
            return nil
        }
        
        att.yy_font = kFont14
        att.yy_color = kTitleColor
        att.yy_lineSpacing = 8
        
        return att
    }
    
    
    @IBAction func bookingHotelAction(_ sender: Any) {
        
        let sb = UIStoryboard(name: "Booking", bundle: nil)
        let bookingVC = sb.instantiateViewController(withIdentifier: "BookingHotelVC")
        
        self.navigationController?.pushViewController(bookingVC, animated: true)
    }
    
}

extension HotelDetailVC: UITableViewDataSource, UITableViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetY = scrollView.contentOffset.y
        
        self.headView.setFactor(offset: offsetY)
        
        if offsetY > 0 {
            
            let alpha = offsetY / CGFloat(headHeight - kNavigationBarHeight)
            
            self.setNavBarStyle(.blackAlpha(alpha))
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "HotelDetailTitleCell") as! HotelDetailTitleCell
            
            return cell
        }
        
        if indexPath.row == 1 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "HotelContactCell") as! HotelContactCell
            
            cell.contact = contact
            
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HotelFacilitiesCell") as! HotelFacilitiesCell
        
        return cell
        
        
    }
}
